// Copyright (c) 2023- All flutter_graph_view authors. All rights reserved.
//
// This source code is licensed under Apache 2.0 License.

import 'package:flutter/material.dart';
import 'package:flutter_graph_view/flutter_graph_view.dart';

import 'data.dart';
import 'execution_plan_convertor.dart';
import 'flow_layout.dart';
import 'plan_node_shape.dart';

class ExecutionPlanDemo extends StatelessWidget {
  final ResultSet data = ResultSet();
  ExecutionPlanDemo({super.key});

  @override
  Widget build(BuildContext context) {
    data.plan = ExecutionPlan.fromRawJson(planStr);
    return FlutterGraphWidget(
      data: data.plan,
      algorithm: FlowLayout(),
      convertor: ExecutionPlanConvertor(),
      options: Options()
        ..useLegend = false
        ..vertexShape = PlanNodeShape()
        ..graphStyle = (GraphStyle()
          ..tagColorByIndex = [
            Colors.red.shade200,
            Colors.orange.shade200,
            Colors.yellow.shade200,
            Colors.green.shade200,
            Colors.blue.shade200,
            Colors.blueAccent.shade200,
            Colors.purple.shade200,
            Colors.pink.shade200,
            Colors.blueGrey.shade200,
            Colors.deepOrange.shade200,
          ])
        ..backgroundBuilder = ((context) => ColoredBox(
              color: Colors.grey.shade800,
              // child: logo,
            )),
    );
  }
}

var planStr =
    r"""{"format":"row","nodeIndexMap":{"14":21,"17":20,"12":19,"13":18,"15":17,"16":16,"19":14,"32":12,"3":11,"30":2,"31":13,"8":4,"2":8,"23":1,"24":0,"1":9,"28":15,"5":6,"27":5,"4":7,"29":3,"6":10},"nodes":[{"dependencies":[23],"id":24,"name":"DataCollect","outputVar":"{\"colNames\":[\"v1\",\"r\",\"v2\"],\"type\":\"DATASET\",\"name\":\"__DataCollect_24\"}","description":[{"name":"inputVar","value":"[{\"colNames\":[\"v1\",\"r\",\"v2\"],\"type\":\"DATASET\",\"name\":\"__Union_23\"}]"},{"name":"kind","value":"ROW"}],"profiles":[{"execDurationInUs":9,"totalDurationInUs":12,"rows":200,"otherStats":null}]},{"dependencies":[30,32],"id":23,"name":"Union","outputVar":"{\"colNames\":[\"v1\",\"r\",\"v2\"],\"type\":\"DATASET\",\"name\":\"__Union_23\"}","description":[{"name":"inputVar","value":"{\"rightVar\":\"__Limit_22\",\"leftVar\":\"__Limit_11\"}"}],"profiles":[{"execDurationInUs":14,"totalDurationInUs":14,"rows":200,"otherStats":null}]},{"dependencies":[29],"id":30,"name":"Project","outputVar":"{\"colNames\":[\"v1\",\"r\",\"v2\"],\"type\":\"DATASET\",\"name\":\"__Limit_11\"}","description":[{"name":"inputVar","value":"__Limit_29"},{"name":"columns","value":"[\"$-.v1 AS v1\",\"$-.r[0] AS r\",\"$-.v2 AS v2\"]"}],"profiles":[{"execDurationInUs":223,"totalDurationInUs":223,"rows":100,"otherStats":null}]},{"dependencies":[8],"id":29,"name":"Limit","outputVar":"{\"colNames\":[\"v1\",\"r\",\"v2\"],\"type\":\"DATASET\",\"name\":\"__Limit_29\"}","description":[{"name":"inputVar","value":"__AppendVertices_8"},{"name":"offset","value":"0"},{"name":"count","value":"100"}],"profiles":[{"execDurationInUs":55,"totalDurationInUs":56,"rows":100,"otherStats":null}]},{"dependencies":[27],"id":8,"name":"AppendVertices","outputVar":"{\"colNames\":[\"v1\",\"r\",\"v2\"],\"type\":\"DATASET\",\"name\":\"__AppendVertices_8\"}","description":[{"name":"inputVar","value":"__Traverse_7"},{"name":"space","value":"62"},{"name":"dedup","value":"true"},{"name":"limit","value":"-1"},{"name":"filter","value":""},{"name":"orderBy","value":"[]"},{"name":"src","value":"none_direct_dst($-.r)"},{"name":"props","value":"[{\"props\":[\"name\",\"age\",\"add\",\"_tag\"],\"tagId\":63},{\"props\":[\"name\",\"_tag\"],\"tagId\":64},{\"props\":[\"a\",\"b\",\"_tag\"],\"tagId\":91}]"},{"name":"exprs","value":""},{"name":"vertex_filter","value":""},{"name":"if_track_previous_path","value":"true"}],"profiles":[{"execDurationInUs":332,"totalDurationInUs":3746,"rows":154,"otherStats":{"\"storaged0\":9779 exec/total":"360(us)/2051(us)","\"storaged2\":9779 exec/total":"394(us)/3352(us)","total_rpc":"3467(us)","\"storaged1\":9779 exec/total":"328(us)/2199(us)"}}]},{"dependencies":[5],"id":27,"name":"Traverse","outputVar":"{\"colNames\":[\"v1\",\"r\"],\"type\":\"DATASET\",\"name\":\"__Traverse_7\"}","description":[{"name":"inputVar","value":"__Dedup_5"},{"name":"space","value":"62"},{"name":"dedup","value":"true"},{"name":"limit","value":"-1"},{"name":"filter","value":""},{"name":"orderBy","value":"[]"},{"name":"src","value":"$_vid"},{"name":"edgeTypes","value":"[]"},{"name":"edgeDirection","value":"BOTH"},{"name":"vertexProps","value":"[{\"props\":[\"name\",\"age\",\"add\",\"_tag\"],\"tagId\":63},{\"props\":[\"name\",\"_tag\"],\"tagId\":64},{\"props\":[\"a\",\"b\",\"_tag\"],\"tagId\":91}]"},{"name":"edgeProps","value":"[{\"props\":[\"_src\",\"_type\",\"_rank\",\"_dst\",\"start_year\",\"end_year\"],\"type\":-65},{\"props\":[\"_src\",\"_type\",\"_rank\",\"_dst\",\"start_year\",\"end_year\"],\"type\":65}]"},{"name":"statProps","value":""},{"name":"exprs","value":""},{"name":"random","value":"false"},{"name":"steps","value":""},{"name":"vertex filter","value":""},{"name":"edge filter","value":""},{"name":"if_track_previous_path","value":"false"},{"name":"first step filter","value":"player._tag IS NOT EMPTY"}],"profiles":[{"execDurationInUs":1375,"totalDurationInUs":9014,"rows":154,"otherStats":{"step 1":"{\n{\"storaged1\":9779 exec/total/vertices: 1248(us)/4818(us)/21,\ntotal_rpc_time: 7611(us)\nstorage_detail: {FilterNode:854(us),GetNeighborsNode:982(us),HashJoinNode:802(us),RelNode:984(us),SingleEdgeNode:376(us),TagNode:355(us)}\n}{\"storaged2\":9779 exec/total/vertices: 5008(us)/7403(us)/38,\ntotal_rpc_time: 7611(us)\nstorage_detail: {FilterNode:1865(us),GetNeighborsNode:2636(us),HashJoinNode:1787(us),RelNode:2640(us),SingleEdgeNode:876(us),TagNode:780(us)}\n}{\"storaged0\":9779 exec/total/vertices: 6030(us)/7349(us)/22,\ntotal_rpc_time: 7611(us)\nstorage_detail: {FilterNode:2903(us),GetNeighborsNode:3020(us),HashJoinNode:2838(us),RelNode:3022(us),SingleEdgeNode:585(us),TagNode:480(us)}\n}\n}"}}]},{"dependencies":[4],"id":5,"name":"Dedup","outputVar":"{\"colNames\":[\"_vid\"],\"type\":\"DATASET\",\"name\":\"__Dedup_5\"}","description":[{"name":"inputVar","value":"__Union_4"}],"profiles":[{"execDurationInUs":71,"totalDurationInUs":71,"rows":81,"otherStats":null}]},{"dependencies":[2,3],"id":4,"name":"Union","outputVar":"{\"colNames\":[\"_vid\"],\"type\":\"DATASET\",\"name\":\"__Union_4\"}","description":[{"name":"inputVar","value":"{\"rightVar\":\"__Project_3\",\"leftVar\":\"__Project_2\"}"}],"profiles":[{"execDurationInUs":18,"totalDurationInUs":19,"rows":308,"otherStats":null}]},{"dependencies":[1],"id":2,"name":"Project","outputVar":"{\"colNames\":[\"_vid\"],\"type\":\"DATASET\",\"name\":\"__Project_2\"}","description":[{"name":"inputVar","value":"__IndexScan_1"},{"name":"columns","value":"[\"$-._src\"]"}],"profiles":[{"execDurationInUs":65,"totalDurationInUs":65,"rows":154,"otherStats":null}]},{"dependencies":[6],"id":1,"name":"IndexScan","outputVar":"{\"colNames\":[\"_src\",\"_dst\"],\"type\":\"DATASET\",\"name\":\"__IndexScan_1\"}","description":[{"name":"inputVar","value":""},{"name":"space","value":"62"},{"name":"dedup","value":"false"},{"name":"limit","value":"9223372036854775807"},{"name":"filter","value":""},{"name":"orderBy","value":"[]"},{"name":"schemaId","value":"65"},{"name":"isEdge","value":"true"},{"name":"returnCols","value":"[\"_src\",\"_dst\"]"},{"name":"indexCtx","value":"[{\"columnHints\":[],\"filter\":\"\",\"index_id\":77}]"}],"profiles":[{"execDurationInUs":0,"totalDurationInUs":3649,"rows":154,"otherStats":{"\"storaged2\":9779 exec/total":"458(us)/2711(us)","\"storaged1\":9779 exec/total":"459(us)/2721(us)","\"storaged0\":9779 exec/total":"412(us)/3414(us)","storage_detail":"{IndexEdgeScanNode(IndexID=77, Path=()):233(us),IndexLimitNode(limit=9223372036854775807):261(us),IndexProjectionNode(projectColumn=[_src,_dst]):257(us)}"}}]},{"dependencies":[],"id":6,"name":"Start","outputVar":"{\"colNames\":[],\"type\":\"DATASET\",\"name\":\"__Start_6\"}","description":[],"profiles":[{"execDurationInUs":0,"totalDurationInUs":34,"rows":0,"otherStats":null}]},{"dependencies":[1],"id":3,"name":"Project","outputVar":"{\"colNames\":[\"_vid\"],\"type\":\"DATASET\",\"name\":\"__Project_3\"}","description":[{"name":"inputVar","value":"__IndexScan_1"},{"name":"columns","value":"[\"$-._dst\"]"}],"profiles":[{"execDurationInUs":80,"totalDurationInUs":81,"rows":154,"otherStats":null}]},{"dependencies":[31],"id":32,"name":"Project","outputVar":"{\"colNames\":[\"v1\",\"r\",\"v2\"],\"type\":\"DATASET\",\"name\":\"__Limit_22\"}","description":[{"name":"inputVar","value":"__Limit_31"},{"name":"columns","value":"[\"$-.v1 AS v1\",\"$-.r[0] AS r\",\"$-.v2 AS v2\"]"}],"profiles":[{"execDurationInUs":381,"totalDurationInUs":383,"rows":100,"otherStats":null}]},{"dependencies":[19],"id":31,"name":"Limit","outputVar":"{\"colNames\":[\"v1\",\"r\",\"v2\"],\"type\":\"DATASET\",\"name\":\"__Limit_31\"}","description":[{"name":"inputVar","value":"__AppendVertices_19"},{"name":"offset","value":"0"},{"name":"count","value":"100"}],"profiles":[{"execDurationInUs":109,"totalDurationInUs":111,"rows":100,"otherStats":null}]},{"dependencies":[28],"id":19,"name":"AppendVertices","outputVar":"{\"colNames\":[\"v1\",\"r\",\"v2\"],\"type\":\"DATASET\",\"name\":\"__AppendVertices_19\"}","description":[{"name":"inputVar","value":"__Traverse_18"},{"name":"space","value":"62"},{"name":"dedup","value":"true"},{"name":"limit","value":"-1"},{"name":"filter","value":""},{"name":"orderBy","value":"[]"},{"name":"src","value":"none_direct_dst($-.r)"},{"name":"props","value":"[{\"props\":[\"name\",\"age\",\"add\",\"_tag\"],\"tagId\":63},{\"props\":[\"name\",\"_tag\"],\"tagId\":64},{\"props\":[\"a\",\"b\",\"_tag\"],\"tagId\":91}]"},{"name":"exprs","value":""},{"name":"vertex_filter","value":""},{"name":"if_track_previous_path","value":"true"}],"profiles":[{"execDurationInUs":387,"totalDurationInUs":2653,"rows":154,"otherStats":{"\"storaged0\":9779 exec/total":"433(us)/2074(us)","\"storaged2\":9779 exec/total":"421(us)/1497(us)","total_rpc":"2345(us)","\"storaged1\":9779 exec/total":"480(us)/2239(us)"}}]},{"dependencies":[16],"id":28,"name":"Traverse","outputVar":"{\"colNames\":[\"v1\",\"r\"],\"type\":\"DATASET\",\"name\":\"__Traverse_18\"}","description":[{"name":"inputVar","value":"__Dedup_16"},{"name":"space","value":"62"},{"name":"dedup","value":"true"},{"name":"limit","value":"-1"},{"name":"filter","value":""},{"name":"orderBy","value":"[]"},{"name":"src","value":"$_vid"},{"name":"edgeTypes","value":"[]"},{"name":"edgeDirection","value":"BOTH"},{"name":"vertexProps","value":"[{\"props\":[\"name\",\"age\",\"add\",\"_tag\"],\"tagId\":63},{\"props\":[\"name\",\"_tag\"],\"tagId\":64},{\"props\":[\"a\",\"b\",\"_tag\"],\"tagId\":91}]"},{"name":"edgeProps","value":"[{\"props\":[\"_src\",\"_type\",\"_rank\",\"_dst\",\"start_year\",\"end_year\"],\"type\":-65},{\"props\":[\"_src\",\"_type\",\"_rank\",\"_dst\",\"start_year\",\"end_year\"],\"type\":65}]"},{"name":"statProps","value":""},{"name":"exprs","value":""},{"name":"random","value":"false"},{"name":"steps","value":""},{"name":"vertex filter","value":""},{"name":"edge filter","value":""},{"name":"if_track_previous_path","value":"false"},{"name":"first step filter","value":"player._tag IS NOT EMPTY"}],"profiles":[{"execDurationInUs":1275,"totalDurationInUs":8935,"rows":154,"otherStats":{"step 1":"{\n{\"storaged1\":9779 exec/total/vertices: 2116(us)/7471(us)/21,\ntotal_rpc_time: 7632(us)\nstorage_detail: {FilterNode:727(us),GetNeighborsNode:853(us),HashJoinNode:677(us),RelNode:854(us),SingleEdgeNode:340(us),TagNode:277(us)}\n}{\"storaged2\":9779 exec/total/vertices: 2394(us)/7101(us)/38,\ntotal_rpc_time: 7632(us)\nstorage_detail: {FilterNode:1854(us),GetNeighborsNode:2074(us),HashJoinNode:1779(us),RelNode:2078(us),SingleEdgeNode:832(us),TagNode:825(us)}\n}{\"storaged0\":9779 exec/total/vertices: 5526(us)/7058(us)/22,\ntotal_rpc_time: 7632(us)\nstorage_detail: {FilterNode:1131(us),GetNeighborsNode:1247(us),HashJoinNode:1066(us),RelNode:1249(us),SingleEdgeNode:559(us),TagNode:433(us)}\n}\n}"}}]},{"dependencies":[15],"id":16,"name":"Dedup","outputVar":"{\"colNames\":[\"_vid\"],\"type\":\"DATASET\",\"name\":\"__Dedup_16\"}","description":[{"name":"inputVar","value":"__Union_15"}],"profiles":[{"execDurationInUs":70,"totalDurationInUs":71,"rows":81,"otherStats":null}]},{"dependencies":[13,14],"id":15,"name":"Union","outputVar":"{\"colNames\":[\"_vid\"],\"type\":\"DATASET\",\"name\":\"__Union_15\"}","description":[{"name":"inputVar","value":"{\"rightVar\":\"__Project_14\",\"leftVar\":\"__Project_13\"}"}],"profiles":[{"execDurationInUs":23,"totalDurationInUs":24,"rows":308,"otherStats":null}]},{"dependencies":[12],"id":13,"name":"Project","outputVar":"{\"colNames\":[\"_vid\"],\"type\":\"DATASET\",\"name\":\"__Project_13\"}","description":[{"name":"inputVar","value":"__IndexScan_12"},{"name":"columns","value":"[\"$-._src\"]"}],"profiles":[{"execDurationInUs":55,"totalDurationInUs":56,"rows":154,"otherStats":null}]},{"dependencies":[17],"id":12,"name":"IndexScan","outputVar":"{\"colNames\":[\"_src\",\"_dst\"],\"type\":\"DATASET\",\"name\":\"__IndexScan_12\"}","description":[{"name":"inputVar","value":""},{"name":"space","value":"62"},{"name":"dedup","value":"false"},{"name":"limit","value":"9223372036854775807"},{"name":"filter","value":""},{"name":"orderBy","value":"[]"},{"name":"schemaId","value":"65"},{"name":"isEdge","value":"true"},{"name":"returnCols","value":"[\"_src\",\"_dst\"]"},{"name":"indexCtx","value":"[{\"columnHints\":[],\"filter\":\"\",\"index_id\":77}]"}],"profiles":[{"execDurationInUs":0,"totalDurationInUs":3535,"rows":154,"otherStats":{"\"storaged2\":9779 exec/total":"485(us)/2422(us)","\"storaged1\":9779 exec/total":"458(us)/2241(us)","\"storaged0\":9779 exec/total":"486(us)/3303(us)","storage_detail":"{IndexEdgeScanNode(IndexID=77, Path=()):75(us),IndexLimitNode(limit=9223372036854775807):89(us),IndexProjectionNode(projectColumn=[_src,_dst]):87(us)}"}}]},{"dependencies":[],"id":17,"name":"Start","outputVar":"{\"colNames\":[],\"type\":\"DATASET\",\"name\":\"__Start_17\"}","description":[],"profiles":[{"execDurationInUs":0,"totalDurationInUs":21,"rows":0,"otherStats":null}]},{"dependencies":[12],"id":14,"name":"Project","outputVar":"{\"colNames\":[\"_vid\"],\"type\":\"DATASET\",\"name\":\"__Project_14\"}","description":[{"name":"inputVar","value":"__IndexScan_12"},{"name":"columns","value":"[\"$-._dst\"]"}],"profiles":[{"execDurationInUs":72,"totalDurationInUs":73,"rows":154,"otherStats":null}]}]}""";
